//
//  ChangePassVC.swift
//  DollarBuy
//
//  Created by 郑东喜 on 2016/11/9.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit

class ChangePassVC: UIViewController {
    
    
    

    //原来密码
    @IBOutlet weak var tfNewPass: UITextField!
    
    //新密码
    @IBOutlet weak var tfOldPass: UITextField!
    
    let manager = AFHTTPSessionManager()
    
    
    //修改密码
    
    @IBAction func changePassSEL(_ sender: UIButton) {

        if localSave.object(forKey: userToken) as? String != nil {
            let token = localSave.object(forKey: userToken) as! String
                print(token)
            if self.tfOldPass.text?.characters.count == 0 {
                MBManager.showBriefAlert(tfPassNull)
                
            } else if self.tfNewPass.text?.characters.count == 0 {
                MBManager.showBriefAlert(passNewNil)
            } else {
                
                
                let param = ["oldpwd" : Encryption.md5Encrypt(with: self.tfOldPass.text),
                             "newpwd" : Encryption.md5Encrypt(with: self.tfNewPass.text),
                             "token": token,
                             ] as [String : Any]
                
                print(param)
                
                
                MBManager.showBriefMessage("密码修改中...", in: self.view)
                let url = "\(xgPass)\("?")\("oldpwd=")\(Encryption.md5Encrypt(with: self.tfOldPass.text)!)\("&")\("newpwd=")\(Encryption.md5Encrypt(with: self.tfNewPass.text)!)\("&")\("token=")\(token)"
                
                print(url)

//                manager.post(url, parameters: nil, success: { (task, response) in
//                    print("\((#file as NSString).lastPathComponent):(\(#line))")
//                    let json = JSON(response:response!)
//                    
//                    if json["resultmsg"] == "密码修改成功" {
//                        MBManager.hideAlert()
//                        MBManager.showBriefAlert("密码修改成功")
//                    } else {
//                        if json["resultmsg"] == "原始密码错误" {
//                            MBManager.hideAlert()
//                            MBManager.showBriefAlert(passNotCor)
//                        }
//                    }
//                    print(json)
//                    
//                    
//                }) { (task, error) in
//                    print("\((#file as NSString).lastPathComponent):(\(#line))")
//                    print(error)
//                }
                NetTool.shareInstance.postWithPath(path: url, paras: nil, success: { (data) in
                    print(data)
                }, failure: { (error) in
                    print(error.localizedDescription)
                })
                
                
                
            }

        } else {
            print("token不存在")
            return
        }
        
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
}

// MARK:- 接收值
extension ChangePassVC {

    func getToken() {
        //接受通知监听
        NotificationCenter.default.addObserver(self, selector:#selector(didMsgRecv(notification:)),name: NSNotification.Name(rawValue: "notifyChatMsgRecv"), object: nil)
    }
    //通知处理函数
    func didMsgRecv(notification:NSNotification){
        
        print("\((#file as NSString).lastPathComponent):(\(#line))")
         print("didMsgRecv: \(notification.userInfo)")
    }

}
